package com.pingancar.creditmanage.dao;

import com.pingancar.creditmanage.dao.BaseDAO;
import com.pingancar.creditmanage.pojo.PaservicePojo;
import com.pingancar.creditmanage.util.myenum.PAServiceField;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
/**
 *Created by system on Tue Sep 09 11:17:13 CST 2014
 */
@Repository
public class PAServiceDaoImpl extends BaseDAO<PaservicePojo> {

    public List<PaservicePojo> queryPAService(List<PAServiceField> paServiceFieldsList, List<String> valuesList) {
        String query = "from PaservicePojo temp where ";
        for(int i = 0; i < paServiceFieldsList.size(); i++){
            if( i != 0 ){
                query += " and ";
            }
            if(valuesList.get(i).isEmpty())
                continue;
            query += "temp.";
            query += paServiceFieldsList.get(i).toString().toLowerCase();
            query += " = ";
            query += valuesList.get(i).toString();
        }

        return  (List<PaservicePojo>) getSession().createQuery(query).list();
    }

    public List<String> getAllTags(String paserviceinfoid) {
        List<String> tagList = new ArrayList<String>();
        List<PaservicePojo> paList = this.findByProperty("paserviceinfoid", paserviceinfoid);
        if(paList != null){
              for(int i=0; i<paList.size(); i++){
                  if(!tagList.contains(paList.get(i).getTag1()))
                    tagList.add(paList.get(i).getTag1());
              }
        }
        return tagList;
    }


	public Integer save(PaservicePojo paservice){
		return (Integer)super.save(paservice);
	}


    public List<PaservicePojo> getPaserviceByShop(Integer id, Integer type, Integer offset, Integer len) {
        String hql = "select ps from PaservicePojo ps, ShopinfoPojo si where ps.shopinfo.id = si.id and si.id = :id and ps.stop = 0";
        if (type != -1)
            hql += " and ps.type.id = :type";
        Query q = getSession().createQuery(hql);
        q.setInteger("id", id);
        if (type != -1)
            q.setInteger("type", type);

        if (len > 0) {
            q.setFirstResult(offset).setMaxResults(len);
        }

        return q.list();
    }

    /**
     * 新增服务检查标签
     * @param tags
     * @param type
     * @return
     */
    public String checkAddTag(String tags, Integer type) {
        if (tags.equals("-1"))
            return ""+type;

        String[] tag = tags.split(" ");
        boolean test = false;
        for (String s : tag) {
            if (s.equals(""+type)) {
                test = true;
                break;
            }
        }

        if (!test)
            tags += " "+type;
        return tags;
    }

    /**
     * 停止服务检查标签
     * @param tags
     * @param type
     * @return
     */
    public String checkRemoveTag(String tags, Integer type, Integer shopid) {
        List l = getPaserviceByShop(shopid, type, 0, -1);
        if (l != null && !l.isEmpty()) {
            return tags;
        }

        String[] tag = tags.split(" ");

        StringBuilder stringBuilder = new StringBuilder();
        boolean first = true;
        for (String s : tag) {
            if (s.equals(""+type))
                continue;
            if (first) {
                first = false;
                stringBuilder.append(s);
            } else {
                stringBuilder.append(" ").append(s);
            }
        }

        if (first)
            return "-1";
        return stringBuilder.toString();
    }
}